package com.xiaochong.tir.manager.controller;

import com.github.pagehelper.PageInfo;
import com.xiaochong.service.common.config.redis.RedisServer;
import com.xiaochong.tir.common.data.po.CoinSummarize;
import com.xiaochong.tir.common.data.po.CoinSummarizeAffiliated;
import com.xiaochong.tir.common.util.RedisKeysUtils;
import com.xiaochong.tir.manager.service.CoinSummarizeAffiliatedService;
import com.xiaochong.tir.manager.service.CoinSummarizeService;
import com.xiaochong.tir.manager.util.ManagerVersion;
import com.xiaochong.tir.manager.util.ReturnResult;
import com.xiaochong.tir.manager.util.SessionUtil;
import com.xiaochong.tir.common.data.vo.CoinSummarizeList;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @Author: WangXing
 * @Description:编辑概括（项目概括）
 * @CreateDate: 2018/6/14 15:11
 */
@Controller
@RequestMapping(value = ManagerVersion.API_V1_PREFIX+"summarize/")
@Api(value = "编辑概括")
public class CoinSummarizeController {

    @Autowired
    private CoinSummarizeService coinSummarizeService;
    @Autowired
    private CoinSummarizeAffiliatedService coinSummarizeAffiliatedService;
    @Autowired
    private RedisServer redisServer;
    /**
     * 编辑概括列表页
     * @return
     */
    @GetMapping("coinsunPage")
    public Object coinSummarizePage(){
        return "/coinsummarize/coinSummarizeList";
    }

    /**
     * 编辑概括列表
     * @param pageSize
     * @param pageIndex
     * @param symbol
     * @return
     */
    @ApiOperation(value = "编辑概括列表",notes = "编辑概括列表")
    @PostMapping("coinsunList")
    @ResponseBody
    public Object coinSummarizeList(@RequestParam("limit")Integer pageSize,@RequestParam("page") Integer pageIndex,
                                    @RequestParam(value = "symbol",required = false)String symbol){
        PageInfo<CoinSummarizeList> lists = coinSummarizeAffiliatedService.selectCoinsunList(pageSize,pageIndex,symbol);
        ReturnResult<CoinSummarizeList> result = new ReturnResult<CoinSummarizeList>(0,"成功",lists.getList(),lists.getTotal());
        return result;
    }

    /**
     * 新增编辑概括页面
     * @param currencyInfoId
     * @param model
     * @return
     */
    @GetMapping("addSumarPage")
    public Object CreCoinSummarize(@RequestParam("id") Integer currencyInfoId,
                                   Model model){
        model.addAttribute("currencyInfoId",currencyInfoId);
        return "/coinsummarize/addencapsulates";
    }

    /**
     * 新增编辑概括
     * @param cs
     * @return
     */
    @ApiOperation(value = "新增编辑概括",notes = "新增编辑概括")
    @PostMapping("addSummarize")
    public String insertCoinSummarize(CoinSummarize cs){
        redisServer.set(RedisKeysUtils.LOGINNAME,"新增编辑概括");
        CoinSummarize summarize = coinSummarizeService.selectByEntity(cs);
        if(null !=summarize){
            return "currencymanage/currencyList";
        }
        //编辑概括信息
        cs.setCreateTime(new Date());
        cs.setUpdateTime(new Date());
        coinSummarizeService.insertSelective(cs);
        //路线信息
        CoinSummarizeAffiliated csa =new CoinSummarizeAffiliated();
        if(StringUtils.isNotBlank(cs.getCourseExt())){
            csa.setCoinSummarizeId(cs.getId());
            csa.setCreateTime(new Date());
            csa.setUpdateTime(new Date());
            csa.setExt(cs.getCourseExt());
            csa.setAffiliatedType(String.valueOf(2));
            coinSummarizeAffiliatedService.insertSelective(csa);
        }

        //人物信息
        if(StringUtils.isNotBlank(cs.getPersonExt())){
            csa.setCoinSummarizeId(cs.getId());
            csa.setCreateTime(new Date());
            csa.setUpdateTime(new Date());
            csa.setExt(cs.getPersonExt());
            csa.setAffiliatedType(String.valueOf(1));
            coinSummarizeAffiliatedService.insertSelective(csa);
        }
        return "currencymanage/currencyList";
    }

    /**
     * 修改编辑概括页面
     * @param id
     * @param model
     * @return
     */
    @GetMapping("encapsulate")
    public String encapsulates(@RequestParam("id") Integer id, Model model){
        //查询编辑概括信息
        CoinSummarize coinSummarize =
                coinSummarizeService.selectByEntity(new CoinSummarize().setCurrencyInfoId(id));
        if(null != coinSummarize.getZeAffiliate()){
            for(CoinSummarizeAffiliated item:coinSummarize.getZeAffiliate()){
                if(item.getAffiliatedType().equals("1")){
                    if(StringUtils.isNotBlank(item.getExt())){
                        coinSummarize.setPersonExt(item.getExt());
                    }
                }else if(item.getAffiliatedType().equals("2")){
                    if(StringUtils.isNotBlank(item.getExt())){
                        coinSummarize.setCourseExt(item.getExt());
                    }
                }
            }
        }
        model.addAttribute("currencyInfoId",id);
        model.addAttribute("summarize",coinSummarize);
        return "/coinsummarize/eidtencapsulates";
    }


    /**
     * 修改编辑概括
     * @param cs
     * @return
     */
    @ApiOperation(value = "修改编辑概括",notes = "修改编辑概括")
    @PostMapping("editSummarize")
    public String updateCoinSummarize(CoinSummarize cs){
        redisServer.set(RedisKeysUtils.LOGINNAME,"修改编辑概括");
        CoinSummarizeAffiliated csa =new CoinSummarizeAffiliated();
        //根据编辑概括的id删除旧的信息
        csa.setCoinSummarizeId(cs.getId());
        coinSummarizeAffiliatedService.deleteByEntity(csa);
        //路线信息
        if(StringUtils.isNotBlank(cs.getCourseExt())){
            csa.setCoinSummarizeId(cs.getId());
            csa.setCreateTime(new Date());
            csa.setUpdateTime(new Date());
            csa.setExt(cs.getCourseExt());
            csa.setAffiliatedType(String.valueOf(2));
            coinSummarizeAffiliatedService.insertSelective(csa);
        }
        //人物信息
        if(StringUtils.isNotBlank(cs.getPersonExt())){
            csa.setCoinSummarizeId(cs.getId());
            csa.setCreateTime(new Date());
            csa.setUpdateTime(new Date());
            csa.setExt(cs.getPersonExt());
            csa.setAffiliatedType(String.valueOf(1));
            coinSummarizeAffiliatedService.insertSelective(csa);
        }
        //修改编辑概括的信息
        cs.setUpdateTime(new Date());
        CoinSummarize old =coinSummarizeService.selectByPrimaryKey(cs.getId());
        coinSummarizeService.updateByPrimaryKey(cs,old);
        return "currencymanage/currencyList";
    }
}
